var express = require('express');
var router = express.Router();
const adminService = require('../service/adminService');
const jwt = require("jsonwebtoken");
const { secretKey } = require('../token');


/**
 * @swagger
 * /admin/getadmin:
 *    get:
 *      tags:
 *      - Naixue
 *      summary: 查询管理员信息
 *      parameters:
 *      - name: current
 *        in: query
 *        description: 当前页码
 *        required: false
 *      - name: pageSize
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
/* 获取管理员数据 */
router.get('/getadmin', async function (req, res) {
   let { current, pageSize,type,value } = req.query;
   let searchParams = {};
   if (type) {
      if (type == "age") {
         searchParams[type] = value;
      } else {
         searchParams[type] = { $regex: value };
      }
   }
   console.log(req.query);
   let data = await adminService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams);
   res.send(data);
});



/**
 * @swagger
 * /admin/findbyid:
 *    get:
 *      tags:
 *      - Naixue
 *      summary: Id查询管理员信息
 *      parameters:
 *      - name: id
 *        in: query
 *        description: 管理员id
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// id查找
router.get('/findbyid', async function (req, res) {
   let { id } = req.query;
   let data = await adminService.findById(id);
   res.send(data);
});



/**
 * @swagger
 * /admin/insert:
 *    post:
 *      tags:
 *      - Naixue
 *      summary: 添加管理员
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                adminName:          
 *                  type: string
 *                  description: 管理员账户
 *                adminPwd:    
 *                  type: string
 *                  description: 管理员密码
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 添加
router.post('/insert', async function (req, res) {
   let { adminName, adminPwd } = req.body;
   console.log(adminName, adminPwd);
   let data = await adminService.insert({ adminName, adminPwd });
   res.send(data);
});



/**
 * @swagger
 * /admin/remove:
 *    delete:
 *      tags:
 *      - Naixue
 *      summary: 删除管理员
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *              - name: id      
 *                  type: string
 *                  description: 管理员Id
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
//删除
router.delete('/remove', async function (req, res) {
   let { id } = req.body;
   console.log(id);
   let data = await adminService.delete(id);
   res.send(data);
});




/**
 * @swagger
 * /admin/update:
 *    put:
 *      tags:
 *      - Naixue
 *      summary: 根据id修改管理员
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                _id:
 *                  type: string
 *                adminName:          
 *                  type: string
 *                adminPwd:    
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 修改
router.put('/update', async function (req, res) {
   let { _id, adminName, adminPwd } = req.body;
   console.log(_id, adminName, adminPwd)
   let data = await adminService.update({ _id, adminName, adminPwd });
   res.send(data);
});

// 登陆
router.post('/login', async function (req, res) {
   let { adminName, adminPwd } = req.body;
   console.log(req.body);
   let data = await adminService.findByUsernameAndPwd({ adminName, adminPwd });
   if (data.length > 0) {
   //   let authToken = jwt.sign({ data: data[0] }, secretKey);
 
   //   req.session.admin = data[0];
     res.send({ status: 1,  });
   //   token: authToken
 
   } else {
     res.send({ status: 0 });
   }
 });


module.exports = router;
